package cn.com.ddapp.ddm.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.com.ddapp.ddm.R;
import cn.com.ddapp.ddm.bean.Coupons;
import cn.com.ddapp.ddm.bean.CouponsEntity;
import cn.com.ddapp.ddm.util.SPUtils;

/**
 * Created by admin on 2016/8/16.
 */
public class CouponsAdapter extends BaseAdapter {

    public List<Coupons> datas;
    Context context;
    private String userId;
    private Integer type;


    public CouponsAdapter(List<Coupons> datas, Context context,Integer type) {
        this.context = context;
        this.datas = datas;
        this.type = type;
        userId = SPUtils.getStringData(context, SPUtils.USERID);
    }

    /**
     * 用来标识当前列表是否处于操作状态 true: 表示长按了，处于操作状态。 false: 非操作状态
     */
    private boolean deleteState = false;

    public boolean isEditable() {
        return deleteState;
    }

    /**
     * 设置列表的显示状态
     *
     * @param mEditable true 删除状态，false为正常的浏览状态
     */
    public void setEditable(boolean mEditable) {
        this.deleteState = mEditable;
        notifyDataSetChanged();
    }

    /**
     * 切换某一个列表项的选中状态
     *
     * @param position 列表项位置
     */
    public void toggleItemCheckedState(int position) {
        Coupons coupons = datas.get(position);
        coupons.mChecked = !coupons.mChecked;
        datas.set(position,coupons);
        notifyDataSetChanged();
    }

    /**
     * 设置列表为全选或全不选
     * @param allChecked true表示全选，false为全不选
     */
    public void setAllItemChecked(boolean allChecked) {
        int count = datas.size();
        for (int i = 0; i < count; i++) {
            Coupons coupons = datas.get(i);
            coupons.mChecked = allChecked;
            datas.set(i,coupons);
        }
        notifyDataSetChanged();
    }

    /**
     * 判断列表是否为全选
     */
    public boolean isAllCheck() {
        boolean isAllcheck = true;
        int count = datas.size();
        for (int i = 0; i < count; i++) {
            Coupons coupons = datas.get(i);
            if(coupons.mChecked==false){
                isAllcheck =  false;
            }
        }
        return isAllcheck;
    }

    /**
     * 获取当前列表中所有已勾选的实体数据
     */
    public ArrayList<Coupons> getCheckedDatas() {

        ArrayList<Coupons> beans = new ArrayList<Coupons>();
        for (Coupons bean : datas) {
            if (bean.mChecked)
                beans.add(bean);
        }
        return beans;
    }

    /**
     * 删除当前列表中已勾选的项目并刷新列表界面
     */
    public void deleteDatasAndUpate() {
        for (int i = datas.size() - 1; i >= 0; i--) {
            if (datas.get(i).mChecked)
                datas.remove(i);
        }
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return datas.size();
    }

    @Override
    public Object getItem(int i) {
        return datas.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View convertView, ViewGroup viewGroup) {
        ViewHolder holder = null;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.coupons_list, null);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        final Coupons conponsList = datas.get(i);
        /*//金额
        private int amount;
        //到期日
        private int dayOffset;
        private String endTimeStr;
        private int id;
        //是否使用
        private int isUsed;
        //金额限制
        private int limitUse;*/

        holder.moneyAmount.setText("￥"+conponsList.getAmount());
        holder.amount.setText(conponsList.getAmount()+"元抵用券");
        holder.limitUse.setText("满"+conponsList.getAmountLimit()+"元可用");
        holder.endTimeStr.setText("有效期至"+conponsList.getValidTime());
        holder.cb.setChecked(conponsList.mChecked);
        //未使用
        if (type==0){
            holder.dayOffset.setText("还有"+conponsList.getOutDays()+"天过期");
        }
        //已使用
        if (type == 1){
            holder.dayOffset.setText("该优惠券已使用");
        }
        //已过期
        if (type==2){
            holder.dayOffset.setText("该优惠券已过期");
        }
        if(isEditable()){
            holder.cb.setVisibility(View.VISIBLE);
        }else {
            holder.cb.setVisibility(View.GONE);
        }
        return convertView;
    }

    public class ViewHolder {
        @BindView(R.id.money_amount)
        TextView moneyAmount;
        @BindView(R.id.amount)
        TextView amount;
        @BindView(R.id.limitUse)
        TextView limitUse;
        @BindView(R.id.dayOffset)
        TextView dayOffset;
        @BindView(R.id.endTimeStr)
        TextView endTimeStr;
        @BindView(R.id.couponitemCheckBox)
        public CheckBox cb;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
